package com.gxuwz.attend.dao.student;

import com.gxuwz.attend.entity.Leave;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;
@Repository

public class LeaveDao {
    @Autowired

    private JdbcTemplate jdbcTemplate;


    // 插入请假申请
    public int insertLeave(Leave leave) {
        String sql = "INSERT INTO sys_leave (leaveID, courseID, reason, daynum, stuNo, applytime, status,audittime,opinion) " +
                "VALUES (?, ?, ?, ?, ?, NOW(), ?,?,?)";
        return jdbcTemplate.update(sql, leave.getLeaveID(), leave.getCourseID(), leave.getReason(),
                leave.getDaynum(), leave.getStuNo(),  leave.getStatus(),leave.getAudittime(),leave.getOpinion());
    }

    // 查询学生所有的请假单



    public List<Leave> getLeaves(String stuID) {
        String sql = "SELECT * FROM sys_leave WHERE stuNo = ?";
        return jdbcTemplate.query(sql, new Object[]{stuID}, (rs, rowNum) -> {
            Leave leave = new Leave();
            leave.setLeaveID(rs.getString("leaveID"));
            leave.setCourseID(rs.getString("courseID"));
            leave.setReason(rs.getString("reason"));
            leave.setDaynum(rs.getInt("daynum"));
            leave.setStuNo(rs.getString("stuNo"));
            leave.setApplytime(String.valueOf(rs.getTimestamp("applytime")));
            leave.setStatus(rs.getString("status"));
            leave.setAudittime(String.valueOf(rs.getTimestamp("audittime")));
            leave.setOpinion(rs.getString("opinion"));

            return leave;
        });
    }

    public int deleteLeave(String leaveID){
        String sql = "delete from sys_leave where leaveID=?";
        return  jdbcTemplate.update(sql,leaveID);

    }

}
